﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace BTSZap
{
    internal class OperationsGroupProxy :IDisposable
    {
        private object instance;
        private bool disposed = false;
        

        public OperationsGroupProxy(string ManagementDBServer, string ManagementDBName)
        {         
            Type typ = Type.GetType("Microsoft.BizTalk.Operations.OperationsGroup,Microsoft.BizTalk.Operations, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
            instance = typ.CreateInstance(ManagementDBServer, ManagementDBName);
        }



        public void TerminateApplicationInstances(string application)
        {
            object instanceFilter = instance.GetType().Assembly.GetType("Microsoft.BizTalk.Operations.InstanceFilter").CreateInstance();
            object applicationFilter = instanceFilter.GetProperty("Application");
            applicationFilter.SetProperty("Value", application);
            instance.VoidCall("TerminateInstances", instanceFilter);
        }

        #region dispose implementation
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    instance.VoidCall("Dispose");                    
                }                
                disposed = true;

            }
        }
        #endregion

    }
}
